var widgets = {
    datepicker: function () {
        return {
            restrict: 'A',
            require:'ngModel',
            link: function (scope, element, attrs,ngModelCtrl) {
                element.datepicker({
                    dateFormat:'yy-mm-dd',
                    changeMonth: true,
                    changeYear: true,
                    onSelect:function (date) {
                        scope.$apply(function () {
                            ngModelCtrl.$modelValue=date.getTime();
                            ngModelCtrl.$setViewValue(date);
                        });
                    }
                });
                scope.$watch(attrs.ngModel,function(){
                    var time=$._getInner(scope,attrs.ngModel);
                    if(time)
                        element.datepicker( "setDate", new Date(time));
                });

            }
        }
    },
    dddatepicker: function () {
        return {
            restrict: 'EA',
            scope:'bind',
            link: function (scope, element, attrs) {
                var _start=$(element).find('.start');
                var _end=$(element).find('.end');
                var view = attrs.view;
                var format = 'yyyy-MM-dd';

                _start.datepicker({
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: format,
                    showButtonPanel: true,
                    closeText: '×',
                    onSelect: function (selectedDate) {
                        var $this = $(this);
                        var date = $this.datepicker('getDate');
                        var time = $._setInner(scope,_start.attr('start'),date.getTime());
                        $this.val(view ?
                            ($._setInner(scope,view,date.format(format)))
                            :time);
                        //_end.datepicker("option","minDate",selectedDate);
                        scope.$apply();
                    }
                });
                _end.datepicker({
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: format,
                    showButtonPanel: true,
                    closeText: '×',
                    onSelect: function (selectedDate) {
                        var $this = $(this);
                        var date = $this.datepicker('getDate');
                        var time = $._setInner(scope,_end.attr('end'),date.getTime());
                        $this.val(view ?
                            ($._setInner(scope,view,date.format(format)))
                            :time);
                        //_start.datepicker("option", "maxDate", selectedDate);
                        scope.$apply();
                    }
                });

                $.datepicker._gotoToday = function (id) {
                    var target = $(id);
                    var inst = this._getInst(target[0]);
                    if (this._get(inst, 'gotoCurrent') && inst.currentDay) {
                        inst.selectedDay = inst.currentDay;
                        inst.drawMonth = inst.selectedMonth = inst.currentMonth;
                        inst.drawYear = inst.selectedYear = inst.currentYear;
                    }
                    else {
                        var date = new Date();
                        inst.selectedDay = date.getDate();
                        inst.drawMonth = inst.selectedMonth = date.getMonth();
                        inst.drawYear = inst.selectedYear = date.getFullYear();
                        this._setDateDatepicker(target, date);
                        this._selectDate(id, this._getDateDatepicker(target));
                    }
                    this._notifyChange(inst);
                    this._adjustDate(target);
                }
            }
        }
    },

    action: function() {
        return {
            restrict: 'A',
            scope: 'bind',
            link: function (scope, element, attrs) {
                element.bind('click', function () {
                    scope.$apply(attrs.action);
                });
            }
        }
    },
    /**
     * action
     */
    icon: function () {
        return {
            restrict: 'E',
            scope: 'bind',
            replace: true,
            template: '<i></i>',
            link: function (scope, element, attrs) {
                element.bind('click', function () {
                    scope.$apply(attrs.action);
                }).css({cursor: 'pointer'});
            }
        }
    }
};